var app = angular.module('dictionaryApp', []);
var keyTip="请输入待检索的关键字";

app.controller('dictionaryController', function($scope, $http, DictionaryService) {
	$scope.load=function(isRest){
		$scope.sel=$scope.sel?$scope.sel:{};
		var pageNum=$scope.pageInfo?isRest==false?$scope.pageInfo.pageNum:1:1;
		var pageSize=$scope.pageInfo?$scope.pageInfo.pageSize:10;
		$scope.sel.pageNum=pageNum;
		$scope.sel.pageSize=pageSize;
		var param={};  
        for(var k in $scope.sel){  
        	param[k] = $scope.sel[k];  
        	if(k=="key"&&$scope.sel[k]==keyTip){
        		delete param[k];
        	}
        
        }
		
		DictionaryService.list(param).success(function (response) {
			$scope.pageInfo=response.data;
			$scope.pageInfo.pageLinks=getPageLinks(response.data.pageNum,response.data.pages);
	    });
	}
	
	$scope.load();
    $scope=pagination.init($scope);
    $scope.dictionaryCreator=function(){
    	redirectUrl("manage/dictionary/","add.jsp");
    }
    $scope.refreshData=function(){
    	$scope.load();
    }
    $scope.lock=function(id){
    	DictionaryService.lock(id).success(function (response) {
    		$scope.load();
    	});
    }
    
    $scope.lockByPid=function(id){
    	DictionaryService.lockByPid(id).success(function (response) {
    		$scope.load();
    	});
    }
    
    $scope.delTip=function(id,mes){
    	$scope.delItem={id:id,mes:mes};
    	$('#box-del').modal({
    		show: true
    	});
    	
    }
    $scope.del=function(id){
    	DictionaryService.del(id).success(function (response) {
    		$('#box-del').modal('hide');
    		$scope.load();
    	});
    }

    if(inputPlaceholder()){
    	$scope.sel.key=keyTip;
    }

});

app.factory('DictionaryService', function ($http) {
	var list = function (param) {
        return $http({
            params: param,
            //url: 'sys/dic/getByPage'
            url: timestamp('sys/dic/getByPage')
            	
        });
    };
    var lock = function (id){
    	return $http.put('sys/dic/get/'+id);
    }
    var lockByPid = function (id) {
    	return $http.put('sys/dic/upStatusByPid/'+id);
    }
    
    var del = function (id){
    	return $http.put('sys/dic/upStatus/'+id);
    }
	return {
		list:function(param){
			return list(param);
		},
		lock:function(id){
			return lock(id);
		},
		lockByPid:function(id){
			return lockByPid(id);
		},
		del:function(id){
			return del(id);
		}
	}
});
